import api from '../request/api.js'
let main = {
  //input输入绑定封装 关键字为data-tag="${params}"
  g_bindInput(e){
    let key = e.currentTarget.dataset.tag
    if(key){
      this.data[key] = e.detail.value
    }else{
      console.error('没有填写绑定关键字')
    }
  },
  //wx.showToast封装
  g_showToast(title='成功',icon='success',time=2000){
    return new Promise((resolve,reject)=>{
      wx.showToast({
        title: title,
        icon: icon,
        duration:time,
        mask:true,
        success:()=>{
          setTimeout(function(){
            resolve()
          },time)
        }
      })
    })
  },
  //wx.showModal封装
  g_showModal({title='提示',content='是否确定'}){
    return new Promise((resolve,reject)=>{
      wx.showModal({
        title:title,
        content:content,
        success:function(res){
          if (res.confirm) {
            resolve()
          }
        },
        fail:function(res){
          reject(res)
        }
      })
    })
  },
  //上传图片
  g_upLoad(filePath,formData){
    return new Promise((resolve,reject)=>{
      wx.uploadFile({
        filePath,
        formData,
        name: 'file',
        url: `${config.data.url}${config.upLoad}`,
        header: {
          'Content-Type': 'multipart/form-data;'
        },
        success(e){
          let data = JSON.parse(e.data)
          resolve(data)
        },
        fail(e){
          reject(e)
        }
      })
    })
  },
  //分页通用方法
  g_handleBranch(data){

    if(data.list.length< data.pageInfo.pageSize){
      this.data.noMore = true
      this.data.noData = false
    }else{
      this.data.noMore = false
      this.data.noData = false
      this.data.page+=1
    }
    if(this.data.page == 1&&data.list.length ==0){
      console.log(data.list.length)
      this.data.noData = true
      this.data.noMore = false
    }
    this.setData({
      noMore:this.data.noMore,
      noData:this.data.noData
    })
  },
  /**
   * 获取页面栈
   * @param {Number} e 
   * return 为this实例 route为页面路劲
   */
  g_getPageInfo(e=2){
    const pages = getCurrentPages();   //获取当前页面信息栈
    const prevPage = pages[pages.length - e] 
    return prevPage
  },
}
module.exports = {
  main
}